Skip to content

Fix stringio content length#7000

Open
killerdevildog wants to merge 2 commits into
psf:mainfrom
killerdevildog:fix-stringio-content-length
Open

Fix stringio content length#7000
killerdevildog wants to merge 2 commits into
psf:mainfrom
killerdevildog:fix-stringio-content-length

Conversation

@killerdevildog
Copy link
Copy Markdown
Contributor

fix(utils): correct Content-Length for io.StringIO bodies (#6917)

  • Add special handling for io.StringIO in super_len() function
  • Calculate byte length by encoding StringIO content as UTF-8
  • Preserve stream position to avoid surprising users
  • Fixes incorrect Content-Length header when StringIO contains multi-byte UTF-8 characters
  • Test multi-byte UTF-8 characters (💩, 🚀, 🎉)
  • Test position preservation after super_len call
  • Test partial read scenarios
  • Test PreparedRequest Content-Length header
  • Test consistency with str and bytes behavior
  • All tests verify correct byte-length calculation"

- Add special handling for io.StringIO in super_len() function
- Calculate byte length by encoding StringIO content as UTF-8
- Preserve stream position to avoid surprising users
- Fixes incorrect Content-Length header when StringIO contains multi-byte UTF-8 characters
- Expand compressed logic into clearer if/else blocks
- Add explanatory comment for remaining bytes calculation
- Maintain same functionality with better code structure
@killerdevildog killerdevildog force-pushed the fix-stringio-content-length branch 2 times, most recently from c8c984b to d48c8a0 Compare February 17, 2026 02:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant